package com.kuang.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @program: juc
 * @description:
 * @author: 陈恩涛
 * @create: 2021-02-22 16:12
 **/
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //new Thread(new Runnable()).start();
        //new Thread(new FutureTask<T>()).start();
        //new Thread(new FutureTask<T>(Callable)).start();

        MyThread thread = new MyThread();
        FutureTask futureTask = new FutureTask(thread); //适配类

        new Thread(futureTask,"A").start(); //怎么启动callable
        new Thread(futureTask,"B").start(); //结果会被缓存，效率高

        Integer o = (Integer) futureTask.get(); //获取Callable的返回结果,这个get方法可能会产生阻塞！把它放到最后，或使用异步通信
        System.out.println(o);
    }
}

class MyThread implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println("call()");
        //耗时操作
        return 1024;
    }
}
